package two_shang._12_03;

import java.io.*;

public class MySpeech {
    public static void main(String[] args) throws IOException {
        File file = new File("X:\\IntelliJ IDEA 2023.1.2\\project\\ClassTest\\src\\two_shang._12_03\\File\\speech.txt");
        String content = "abcdefghijklmnopqrst";
        //创建文件并且写入内容
        if (file.createNewFile() || file.exists()) {
            try {
                //这个BufferedWriter可以切实写入内容到txt文件中,向后添加
                BufferedWriter out = new BufferedWriter(
                        new OutputStreamWriter(new FileOutputStream(file, true)));
                out.write(content);
                out.close();
                System.out.println("文件创建成功并写入内容！" + "\n");
            } catch (IOException e) {
                System.out.println("文件创建或写入内容时出现错误：" + e.getMessage());
            }
        } else {
            System.out.println("Error!");
        }


        int count = 0;
        try (FileInputStream fis = new FileInputStream(file)) {
            int data;
            while ((data = fis.read()) != -1) {
                count++;
                // 这里可以根据需要处理每个字节的逻辑
            }
            System.out.println("文件读取完成，总字节数为：" + count);
        } catch (IOException e) {
            System.out.println("读取文件时出现错误：" + e.getMessage());
        }

    }

}
